
var form_syslog_preinit = function(){
	$('#fd').datetimebox({  
		showSeconds:false,
		mulitple: false,
		editable: false
	});
	$('#td').datetimebox({  
		showSeconds:false,
		mulitple: false,
		editable: false
	});
	$('#cc').combobox({  
		mulitple: false,
		editable: false
	});  
}

var grid_syslog_dbrow = function(index,row){
	var detail = "<h1>Log detail</h1>";
	detail+="<table class='error_table'>"
	// error type
	detail+="<tr><td class='error_title' >Error Type</td><td>"+ getErrorName(row.error_type)+"</td></tr>";
	//description
	detail+="<tr><td class='error_title' >Error Description</td><td>"+row.error_str+"</td></tr>";
	//
	detail+="<tr><td class='error_title' >Error in file</td><td>"+row.error_file+"</td></tr>";
	//
	detail+="<tr><td class='error_title' >Error on line</td><td>"+row.error_line+"</td></tr>";
	//
	detail+="<tr><td class='error_title' >On date</td><td>"+row.error_date_string+"</td></tr>";
	//
	if (row.in_dev == 0)
		detail+="<tr><td class='error_title' >In Development Mode</td><td>No</td></tr>";
	else
		detail+="<tr><td class='error_title' >In Development Mode</td><td>Yes</td></tr>";
	//
	if (row.module_data!="")
		detail+="<tr><td class='error_title' >Module Data note</td><td>"+row.module+data+"</td></tr>";
	//
	// client info
	var obj = eval('(' + row.client_info + ')');
	detail+="<tr><td class='error_title' >Client Info</td><td>"+printObj(obj)+"</td></tr>";
	// client request
	obj = eval('(' + row.dump_request + ')');
	detail+="<tr><td class='error_title' >Request Parameters</td><td>"+printObj(obj)+"</td></tr>";
	
	// Server Info
	obj = eval('(' + row.dump_server + ')');
	detail+="<tr><td class='error_title' >Server Info</td><td>"+printObj(obj)+"</td></tr>";
	
	detail+="</table>"
	$.fancybox( {
		content: detail,
		minWidth: 300,
		minHeight: 200,
		autoScale: true,
		openEffect : 'elastic',
		closeEffect : 'elastic'
	} );
}

var form_onBeforeLoad = function(param)
{   
	jQuery(this).datagrid('options').url =__Manager_Log_BuildUrl() ;
};

var column_error_formatter = function(value, row, index){
	return getErrorName(value);
}

var column_indev_formatter = function(value, row, index){
	if (value == 1)
		return "Yes";
	else
		return "No";
}

var column_clientinfo_formatter = function(value, row, index){
	var obj = eval('(' + value + ')');
	return obj.browser+" "+obj.version+"<br>"+obj.platform+" platform";
}

function __Manager_Log_BuildUrl(){
	var url=__FormHelper_GetActionLink(); 
	
	var options=__FormHelper_GetGridObject().datagrid("options");
	
	var type_error = $('#cc').combobox('getValue');
	if(type_error!="")
		{
		url+="&type_error="+type_error;
	}
	url+="&page="+options.pageNumber+"&pageSize="+options.pageSize;
	var from_date = $('#fd').datetimebox('getValue');
	if(from_date!="")
		{
		url+="&from_date="+from_date;
	}
	var to_date = $('#td').datetimebox('getValue');
	if(to_date!="")
		{
		url+="&to_date="+to_date;
	}
	
	if (options.sortName != null)
	{
		url+="&sortName="+encodeURI(options.sortName)+"&sortOrder="+options.sortOrder;
	}
	
	return url;

}

/**
* Return error code name
*/
function getErrorName(value){
	switch (value)
	{
		case "0":
			return "System Log";
		case "1":
			return "PHP Error";
		case "2":
			return "PHP Warning";
		case "4":
			return "PHP Parse";
		default:
			return "Code "+value;
	}
}

function __Clear_Log()
{
	var msg = "Are you sure you want to clear log?";
	if(!confirm(msg)) return;
	var temp=Math.random()*9999;
	jQuery.post("/index.php", {
		module: 'administrationmodule',
		action: 'log_manage_clearall',
		ajax_action: '1',
		src: __FormHelper_GetSrc(),
		t: temp
	},
	function(param)
	{
		__FormHelper_GetGridObject().datagrid('reload');
	}
	);
	__FormHelper_GetGridObject().datagrid('clearSelections');
}
function __Export_Log()
{
	alert("TODO: NOT DONE")
}

/**
* Dump javascript object by json decode
* @author: trungdt
* @since: Apr, 2012
*/
var printObj = function(o) {
	var str = '';

	for (var p in o) {
		if (typeof o[p] == 'string' || typeof o[p] == 'boolean' || typeof o[p] == 'number') {
			str += p + ': ' + o[p] + '; </br>';
		}
		else {
			str += p + ': { </br>' + printObj(o[p]) + '}';
		}
	}

	return str;
}

